/**
 * 
 */
package com.taobao.snowsnow.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

/**
 * @author qinanhg
 * 创建日期: 2012-3-16 下午1:24:01
 */
public class ReadIni {
	private String filePath;
	private FileInputStream inputStream;
	
	public ReadIni(String filePath) throws IOException {
		this.filePath = filePath;
		File file = new File(filePath);
		
		if(!file.exists()) {
			FileWriter w = new FileWriter(file);
			w.close();
		} 
		
		inputStream = new FileInputStream(filePath);
	}
	
	/**
	 * 获取属性
	 * @param property
	 * @return
	 */
	public String getProperty(String property) {
		
		Properties p = new Properties();
		
		try {
			inputStream = new FileInputStream(filePath);
			p.load(inputStream);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		return p.getProperty(property);
	}
	
	public void saveProperty(String property, long value) throws Exception {
		saveProperty(property, String.valueOf(value));
	}
	
	/**
	 * <p>保存属性</p>
	 * @param property
	 * @param value
	 */
	public void saveProperty(String property, String value) throws Exception {
		
		Properties p = new Properties();
		p.load(inputStream);
		p.setProperty(property, value);
	    FileOutputStream out = new FileOutputStream(filePath, false);
		p.store(out, "save");
	}
	
	public static void main(String[] args) {
		ReadIni r;
		try {
			r = new ReadIni("d:/1.ini");
			r.saveProperty("ssf.awb", "1243");
//			r.saveProperty("sto.awb", "456");
//			r.saveProperty("zjs.awb", "456");
			System.out.println(r.getProperty("sf.awb"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
